<?php


require_once('connect.php');
require_once('curl.php');

$my_file = curl_req("http://4636.ushahidi.com/rss.php?key=yqNm7FHSwfdRb8nC2653&category=6b&limit=0,2000");
parsethatxml($my_file);


$my_file = curl_req("http://4636.ushahidi.com/rss.php?key=yqNm7FHSwfdRb8nC2653&category=4h&limit=0,2000");
parsethatxml($my_file);


//$my_file = file_get_contents("sample.rss");
function parsethatxml($my_file){
    $my_file = preg_replace("/georss:point/", "georsspoint",$my_file);
    $my_file = preg_replace("/>/","RRIGHTT",$my_file);
    $my_file = preg_replace("/</","LLEFTT",$my_file);
    
    $my_file = htmlspecialchars($my_file,ENT_NOQUOTES);
    
    $my_file = preg_replace("/RRIGHTT/",">",$my_file);
    $my_file = preg_replace("/LLEFTT/","<",$my_file);
    
    
    $xml = simplexml_load_string($my_file);
    if(count($xml->entry) > 0){
        foreach($xml->entry as $node){
            $id = $node->id;
            $link = $node->link['href'];
            $author = $node->author->name;
            $updated = $node->updated;
            $title = $node->title;
            $sms = $node->sms;
            $smsrec = $node->smsrec;
            $phone = $node->phone;
            $carrierid = $node->carrierid;
            $carriername = $node->carriername;
            $category = $node->category['term'];
            $categorization = $node->categorization;
            $actionable = $node->actionable;
            $firstname = $node->firstname;
            $lastname = $node->lastname;
            $gender = $node->gender;
            $numppl = $node->numppl;
            $status = $node->status;
            $address = $node->address;
            $city = $node->city;
            $department = $node->department;
            $summary = $node->summary;
            $notes = $node->notes;
            $georsspoint = $node->georsspoint;
            
            insert_entry($id,$link,$author,$updated,$title,$sms,$smsrec,$phone,$carrierid,$carriername,$category,$categorization,$actionable,$firstname,$lastname,$gender,$numppl,$status,$address,$city,$department,$summary,$notes,$georsspoint);
        }
    }
}

function  insert_entry($id,$link,$author,$updated,$title,$sms,$smsrec,$phone,$carrierid,$carriername,$category,$categorization,$actionable,$firstname,$lastname,$gender,$numppl,$status,$address,$city,$department,$summary,$notes,$georsspoint){
    
    //print $category;
    if(preg_match("/6b/",$categorization) || preg_match("/4h/",$categorization)){
        
        $updated = preg_replace("/T/"," ",$updated);
        $updated = preg_replace("/Z/"," ",$updated);
        
        $query = sprintf(
        "INSERT INTO newdata (url,date_retrieved,phone,first_name,last_name,sex,missingstatus,street,city,state,original_text,comment,loc,website_id) VALUES 
        ('%s','%s', '%s','%s', '%s','%s','%s','%s','%s','%s','%s', '%s','%s','9')",
        $link,$updated,$phone,$firstname,$lastname,$gender,$status,$address,$city,$department,$summary,$notes,$georsspoint);
        
        //print $query . "\n";
        
        mysql_query($query);
    
    
    }



}


?>